package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.li.work.mapper.SysUserMapper;
import com.ruoyi.teaching.domain.PlatformUVirtualStudy;
import com.ruoyi.teaching.domain.UniversalCreateUpdate;
import com.ruoyi.teaching.mapper.PlatformUVirtualStudyMapper;
import com.ruoyi.teaching.service.PlatformUVirtualStudyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.teaching.utils.UniversalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2024-07-25
 */
@Service
public class PlatformUVirtualStudyServiceImpl extends ServiceImpl<PlatformUVirtualStudyMapper, PlatformUVirtualStudy> implements PlatformUVirtualStudyService {

    @Autowired
    private PlatformUVirtualStudyMapper platformUVirtualStudyMapper;


    @Override
    public AjaxResult selectPlatformUVirtualStudyList(Long virtualId) {
        // 获取用户ID，如果无法获取则捕获异常
        Integer userId = null;
        try {
            userId = SecurityUtils.getUserId().intValue();
        } catch (ServiceException e) {
            // 无法获取用户ID时，直接返回空
            return new AjaxResult(HttpStatus.SUCCESS,"请登录后查看");
        }
        QueryWrapper<PlatformUVirtualStudy> platformUVirtualStudyQueryWrapper = new QueryWrapper<>();
        platformUVirtualStudyQueryWrapper.eq("virtual_id",virtualId);
        platformUVirtualStudyQueryWrapper.eq("create_id",userId);
        List<PlatformUVirtualStudy> platformUVirtualStudies = platformUVirtualStudyMapper.selectList(platformUVirtualStudyQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",platformUVirtualStudies);
    }

    @Override
    public AjaxResult insertPlatformUVirtualStudy(PlatformUVirtualStudy platformUVirtualStudy) {
        // 获取用户ID，如果无法获取则捕获异常
        Integer userId = null;
        try {
            userId = SecurityUtils.getUserId().intValue();
        } catch (ServiceException e) {
            // 无法获取用户ID时，直接返回空
            return new AjaxResult(HttpStatus.SUCCESS,"请先登录");
        }
        Long createId = SecurityUtils.getUserId();
        platformUVirtualStudy.setCreateId(createId);
        platformUVirtualStudy.setCreateTime(DateUtils.getNowDate());
        int insert = platformUVirtualStudyMapper.insert(platformUVirtualStudy);
        if(insert > 0){
            return new AjaxResult(HttpStatus.SUCCESS,"考核记录成功");
        }else {
            return new AjaxResult(HttpStatus.ERROR,"考核记录失败，请联系管理员");
        }
    }
}
